<template>
  <div class="report">
    <tableTool>
      <div slot="export">
        <el-button type="primary" @click="excelExport">导出</el-button>
      </div>

      <div class="tool-form" slot="form">
        <div class="input-group">
          <span class="lable">设备名称：</span>
          <el-input placeholder="请输入" v-model="search.equipmentName" />
        </div>
        <div class="input-group">
          <span class="lable">设备类别：</span>
          <el-input
            placeholder="请输入"
            v-model="search.equipmentCategoryName"
          />
        </div>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="success" @click="onReset">重置</el-button>
      </div>
    </tableTool>
    <customTable :list="list" :combined="combined">
      <template v-slot:title>
        <tr>
          <th colspan="15">{{ orgName }}</th>
        </tr>
        <tr>
          <th rowspan="2">设备编号</th>
          <th rowspan="2">设备名称</th>
          <th rowspan="2">规格型号</th>
          <th rowspan="2">车牌号</th>
          <th rowspan="2">生产厂商</th>
          <th rowspan="2">出厂编码</th>
          <th rowspan="2">主动力编码</th>
          <th rowspan="2">购置日期</th>
          <th rowspan="2">功率（KW）</th>
          <th colspan="3">金额</th>
          <th rowspan="2">成新率</th>
          <th rowspan="2">存放地</th>
          <th rowspan="2">使用单位</th>
        </tr>
        <tr>
          <th>原值</th>
          <th>本年折旧</th>
          <th>净值</th>
        </tr>
      </template>
      <template #body="{ list }">
        <tr v-for="item in list" :key="item.equipmentSerialNumber">
          <td>{{ item.equipmentSerialNumber }}</td>
          <td>{{ item.equipmentName }}</td>
          <td>{{ item.equipmentModel }}</td>
          <td>{{ item.licensePlate }}</td>
          <td>{{ item.manufacturer }}</td>
          <td>{{ item.factoryNumber }}</td>
          <td>{{ item.mainPowerCode }}</td>
          <td>{{ item.datePurchase }}</td>
          <td>{{ item.power }}</td>
          <td>{{ item.purchasePrice }}</td>
          <td>{{ item.currentYearDepreciation }}</td>
          <td>{{ item.netWorth }}</td>
          <td>{{ item.generationRate }}</td>
          <td>{{ item.placeStorage }}</td>
          <td>{{ item.unitUse }}</td>
        </tr>
      </template>
      <template #empty>
        <tr>
          <td colspan="15" class="empty">暂无数据</td>
        </tr>
      </template>
      <template #footer="{ combined }">
        <tr>
          <td>合计</td>
          <td>{{ combined.equipmentName }}</td>
          <td>{{ combined.equipmentModel }}</td>
          <td>{{ combined.licensePlate }}</td>
          <td>{{ combined.manufacturer }}</td>
          <td>{{ combined.factoryNumber }}</td>
          <td>{{ combined.mainPowerCode }}</td>
          <td>{{ combined.datePurchase }}</td>
          <td>{{ combined.power }}</td>
          <td>{{ combined.purchasePrice }}</td>
          <td>{{ combined.currentYearDepreciation }}</td>
          <td>{{ combined.netWorth }}</td>
          <td>{{ combined.generationRate }}</td>
          <td>{{ combined.placeStorage }}</td>
          <td>{{ combined.unitUse }}</td>
        </tr>
      </template>
    </customTable>
    <pagination
      @onCurrentChange="handleCurrentChange"
      :total="page.total"
      :limit="page.limit"
    ></pagination>
  </div>
</template>
<script>
import tableTool from "../components/tableTool.vue";
import customTable from "../components/customTable.vue";
import pagination from "../components/pagination.vue";
import request from "@/api/comprehensiveReport/equipmentManagement.js";
import { combinedFn } from "@/pages/comprehensiveReport/equipmentManagement/mixin";

export default {
  name: "fixedAssetsStatisticsTableFiliale",
  components: { tableTool, customTable, pagination },
  data() {
    return {
      list: [],
      page: {
        total: 0,
        currentPage: 1,
        limit: 10,
      },
      orgName: "分子公司",
      search: {
        equipmentCategoryName: "",
        equipmentName: "",
      },
      orgId:
        this.$route.query.orgId || this.$store.state.userInfo.orgInfo.orgId,
      combined: {},
      headers: {
        设备编号: "equipmentSerialNumber",
        设备名称: "equipmentName",
        规格型号: "equipmentModel",
        车牌号: "licensePlate",
        生产厂商: "manufacturer",
        出厂编码: "factoryNumber",
        主动力编码: "mainPowerCode",
        购置日期: "datePurchase",
        功率: "power",
        原值: "purchasePrice",
        本年折旧: "currentYearDepreciation",
        净值: "netWorth",
        成新率: "generationRate",
        存放地: "placeStorage",
        使用单位: "unitUse",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onSearch() {
      this.getList();
      this.onReset();
    },
    onReset() {
      this.search = {
        equipmentCategoryName: "",
        equipmentName: "",
      };
    },
    async getList() {
      const res = await request.getConstituentFixedAssets({
        orgId: this.orgId,
        pageSize: this.page.limit,
        pageNo: this.page.currentPage,
        orgType: this.$store.state.userInfo.orgInfo.orgType,
        equipmentCategoryName: this.search.equipmentCategoryName, // 查询设备类别
        equipmentName: this.search.equipmentName, // 查询设备名称
      });
      console.log("发送请求，获取数据", res);
      if (res.data == null) {
        this.list = []
        this.combined = {}
        this.page.total = 0
        return;
      }
      this.page.total = res.totalCount;
      this.orgName = res.data[0].orgName;
      const { list, combined } = combinedFn(
        Object.values(this.headers).slice(1),
        res.data,
        ["purchasePrice", "currentYearDepreciation", "netWorth"]
      );
      this.list = list;
      this.combined = combined;
    },
    handleCurrentChange(currentPage) {
      console.log(`当前页: 第${currentPage}页`);
      this.page.currentPage = currentPage;
      // 请求数据
      this.getList();
    },
    excelExport() {
      const multiHeader = [
        [this.orgName, "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
        [
          "设备编号",
          "设备名称",
          "规格型号",
          "车牌号",
          "生产厂商",
          "出厂编码",
          "主动力编码",
          "购置日期	",
          "功率（KW）",
          "金额",
          "",
          "",
          "成新率",
          "存放地",
          "使用单位",
        ],
      ];
      const merges = [
        "A1:O1",
        "A2:A3",
        "B2:B3",
        "C2:C3",
        "D2:D3",
        "E2:E3",
        "F2:F3",
        "G2:G3",
        "H2:H3",
        "I2:I3",
        "J2:L2",
        "M2:M3",
        "N2:N3",
        "O2:O3",
      ];
      import("@/utils/Export2Excel.js").then(async (excel) => {
        const { data: res } = await request.getConstituentFixedAssets({
          orgId: this.orgId,
          pageSize: this.page.total,
          pageNo: 1,
          orgType: this.$store.state.userInfo.orgInfo.orgType
        });

        const { list, combined } = combinedFn(
          Object.values(this.headers).slice(1),
          res,
          ["purchasePrice", "currentYearDepreciation", "netWorth"]
        );

        const data = this.formatJson(this.headers, list);
        data.push(["合计", ...Object.values(combined)]);
        excel.export_json_to_excel({
          header: Object.keys(this.headers),
          data,
          filename: `设备管理-固定资产统计表-${this.orgName}`,
          autoWidth: true,
          bookType: "xlsx",
          multiHeader,
          merges,
        });
      });
    },
    formatJson(headers, list) {
      return list.map((item) => {
        return Object.values(headers).map((value) => {
          return item[value];
        });
      });
    },
  },
};
</script>
<style lang="scss"></style>
